package com.onenine.app.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.onenine.base.utils.Md5Util;
import com.onenine.common.util.ApUtils;

/**
 * 非user相关的SharedPreferences
 */
public class SpCommonHelper {

    private static final String SP_COMMON = "sp_common";
    private SharedPreferences mCommonSp;
    private static SpCommonHelper mInstance;

    private SpCommonHelper() {
        mCommonSp = ApUtils.getApp()
                .getSharedPreferences(SP_COMMON, Context.MODE_PRIVATE);
    }

    public static SpCommonHelper getInstance() {
        if (mInstance == null) {
            mInstance = new SpCommonHelper();
        }
        return mInstance;
    }

    /**
     * 设置已看过引导
     */
    public void setGuide() {
        SharedPreferences.Editor editor = mCommonSp.edit();
        editor.putBoolean("guide", true).apply();
    }

    /**
     * 是否已看过引导
     *
     * @return
     */
    public boolean isGuide() {
        return mCommonSp.getBoolean("guide", false);
    }

    /**
     * 缓存升级版本与服务端版本是否相同
     *
     * @param versionNum
     * @return
     */
    public boolean isAppVersionMd5Equal(String versionNum) {
        String spMd5 = mCommonSp.getString("update", null);
        return TextUtils.equals(spMd5, Md5Util.getStringMD5(versionNum));
    }

    /**
     * 保存服务端版本md5
     *
     * @param versionNum
     */
    public void setMd5AppVersion(String versionNum) {
        mCommonSp.edit().putString("update", Md5Util.getStringMD5(versionNum)).apply();
    }

    /**
     * 缓存打招呼消息是否读过
     *
     * @param read
     */
    public void applyGreetMsgRead(boolean read) {
        mCommonSp.edit()
                .putBoolean("greetMsgRead", read)
                .apply();
    }

    /**
     * 获取缓存打招呼是否读过
     *
     * @return
     */
    public boolean isGreetMsgRead() {
        return mCommonSp.getBoolean("greetMsgRead", true);
    }

    /**
     * 设置是否忽略消息提醒
     *
     * @param ignore
     */
    public void applyIgnoreMsgNotify(boolean ignore) {
        mCommonSp.edit()
                .putBoolean("ignoreMsgNotify", ignore)
                .apply();
    }

    /**
     * 是否忽略消息提醒
     *
     * @return
     */
    public boolean isIgnoreMsgNotify() {
        return mCommonSp.getBoolean("ignoreMsgNotify", false);
    }

}
